package kr.co.doriclassic.chapter17;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.GridLayout;

public class SwingComponentAll extends JFrame implements ItemListener{

	private JPanel contentPane;
	private JPanel panelImg;
	private JPanel panelChk;
	private JPanel panelRadioBtn;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private JPanel panel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SwingComponentAll frame = new SwingComponentAll();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SwingComponentAll() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
		
		panelImg = new JPanel();
		contentPane.add(panelImg);
		panelImg.setLayout(new BorderLayout(0, 0));
		
		lblNewLabel = new JLabel("이미지를 보려면 아래의 버튼을 누르시오");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panelImg.add(lblNewLabel);
		
		btnNewButton = new JButton("이미지레이블");
		btnNewButton.addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent e) {
				if(lblNewLabel.getIcon()==null){
					ImageIcon icon = new ImageIcon("img.png");
					lblNewLabel.setIcon(icon);
					lblNewLabel.setText(null);
				}								
				else{
					lblNewLabel.setIcon(null);
					lblNewLabel.setText("이미지를 보려면 아래의 버튼을 누르시오");
				}
			}
		});
		panelImg.add(btnNewButton, BorderLayout.SOUTH);
		
		panelChk = new JPanel();
		contentPane.add(panelChk);
		panelChk.setLayout(new GridLayout(0, 4, 0, 0));
		
		panel = new JPanel();
		panelChk.add(panel);
		
		JCheckBox[] chkBtns = new JCheckBox[3];
		String[] strs ={"img_1","img_2","img_3"};
		JLabel[] lbls = new JLabel[3];
		ImageIcon[] icons = new ImageIcon[3];
		
		for (int i = 0; i < 3; i++) {
			chkBtns[i] = new JCheckBox(strs[i]);
			chkBtns[i].addItemListener(this);
			lbls[i] = new JLabel(strs[i]+".jpg");
			icons[i] = new ImageIcon(strs[i]+".jpg");
			
			panelChk.add(chkBtns[i]);
		}		
		
		panelRadioBtn = new JPanel();
		contentPane.add(panelRadioBtn);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		
	}

}
